package com.ebupt.migu.music.autoTri.Service.Impl;

import com.ebupt.migu.music.autoTri.Entity.VipPacketInfo;
import com.ebupt.migu.music.autoTri.Mapper.VipPacketMapper;
import com.ebupt.migu.music.autoTri.Service.VipPacketService;
import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @description: 会员红包模板管理
 * @author: liuyangyang@ebupt.com
 * @date: 2021/6/21 15:39
 */
@Slf4j
@Service
public class VipPacketServiceImpl implements VipPacketService {

    @Autowired
    private VipPacketMapper vipPacketMapper;

    /**
     * 创建红包模板
     *
     * @param map
     * @return
     */
    @Override
    public ResultObject add(Map<String, Object> map) {
        ResultObject res = new ResultObject();
        try {
            String actionId = String.valueOf(map.get("actionId"));
            if (vipPacketMapper.isActionIdExist(actionId)) {
                res.setCode(StatusEnum.VIP_PACKET_EXIST.getCode());
                res.setMsg(StatusEnum.VIP_PACKET_EXIST.getMsg());
                return res;
            }
            vipPacketMapper.add(map);
            log.info("{}添加了会员红包：{}", map.get("creatorName"), actionId);
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg("创建成功");
            return res;
        } catch (Exception e) {
            log.error("创建会员红包失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

    /**
     * 查询会员红包
     *
     * @param map
     * @return
     */
    @Override
    public ResultObject query(Map<String, Object> map) {
        ResultObject<Page<Map<String, Object>>> res = new ResultObject<>();
        try {
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            int total = vipPacketMapper.totalPackets(map);
            List<Map<String, Object>> list = vipPacketMapper.query(map);
            // 分页
            Page<Map<String, Object>> page = new Page<>();
            page.setTotal(total);
            page.setData(list);
            res.setData(page);
            return res;
        } catch (Exception e) {
            log.error("查询会员红包失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

    /**
     * 删除会员红包
     *
     * @param map
     * @return
     */
    @Override
    public ResultObject delete(Map<String, Object> map) {
        ResultObject res = new ResultObject();
        try {
            //当前红包是否已经被策略引用
            if (vipPacketMapper.isVipPacketUsed(String.valueOf(map.get("actionId")))) {
                res.setCode(StatusEnum.VIP_PACKET_IS_USED.getCode());
                res.setMsg(StatusEnum.VIP_PACKET_IS_USED.getMsg());
                return res;
            }
            String deleteOperId = String.valueOf(map.get("creatorId"));
            String deleteOperName = String.valueOf(map.get("creatorName"));
            String deleteOperDept = String.valueOf(map.get("creatorDepartment"));
            VipPacketInfo vipPacketInfo = new VipPacketInfo();
            vipPacketInfo.setDeleteOperId(deleteOperId);
            vipPacketInfo.setDeleteOperName(deleteOperName);
            vipPacketInfo.setDeleteOperDepartment(deleteOperDept);
            vipPacketInfo.setVipPacketStatus(0);
            vipPacketInfo.setId(Integer.valueOf(String.valueOf(map.get("id"))));
            vipPacketMapper.update(vipPacketInfo);
            log.info("{}删除了会员红包，主键ID：{}", deleteOperName, map.get("id"));
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg("删除成功");
            return res;
        } catch (Exception e) {
            log.error("删除会员红包失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }

    /**
     * 通过红包ID查询红包信息
     *
     * @param map
     * @return
     */
    @Override
    public ResultObject queryById(Map<String, Object> map) {
        ResultObject res = new ResultObject();
        try {
            res.setCode(StatusEnum.SUCCESS.getCode());
            res.setMsg(StatusEnum.SUCCESS.getMsg());
            res.setData(vipPacketMapper.queryById(map));
            return res;
        } catch (Exception e) {
            log.error("通过红包ID查询红包详情失败：{}", e.getMessage());
            res.setCode(StatusEnum.ERROR.getCode());
            res.setMsg(StatusEnum.ERROR.getMsg());
            return res;
        }
    }
}
